package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.BreakfastDAO;
import com.wanmait.hotelManage.dao.CarDAO;
import com.wanmait.hotelManage.dao.OrderDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.dao.UserinfoDAO;
import com.wanmait.hotelManage.vo.Breakfast;
import com.wanmait.hotelManage.vo.Order;
import com.wanmait.hotelManage.vo.Pager;

/**
 * Servlet implementation class manageOrderServlet
 */
@WebServlet("/manage/OrderServlet")
public class manageOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
  

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if("getOrder".equals(action))
		{
			this.getOrder(request, response);
		}
		if("init".equals(action))
		{
			this.init(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageSize(8);
		int rows=OrderDAO.getOrderdao().getRows();//�����ҳ��
		pager.setPageCount(rows);//������ҳ��
		pager.setPageNo(1);//Ĭ����ʾ��һҳ
		List<Order> list= OrderDAO.getOrderdao().getOrders(pager);//��һҳ�Ĳ�Ʒ

		List<Double> prices=new ArrayList<Double>();
		for(Order order:list) {
			//Double prices=OrderDAO.getOrderdao().getPrice(order);
			Boolean bflag = order.getBreakfast();
			Boolean carflag = order.getCar();
			
			Breakfast b=BreakfastDAO.getBreakfastdao().findall();
			Double carPrice=CarDAO.getCarDAO().findprice();
			Double discount=UserinfoDAO.getUserinfodao().findDiscountByUserinfoId(order.getUserinfo().getId());
			Double roomTypePrice= RoomTypeDAO.getRoomtypedao().getPriceByRoomTypeTitle(order.getRoomType().getTitle());
			int days=OrderDAO.getOrderdao().getDays(order.getId());
			double totalPrice =0;
			if(bflag) {
				totalPrice+=b.getPrice()*days;
			}
			if(carflag) {
				totalPrice+=carPrice;
			}
			
			totalPrice=(totalPrice+roomTypePrice*days)*discount;
			
			BigDecimal   bbbb   =   new   BigDecimal(totalPrice); 
			totalPrice   =   bbbb.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 
			
			prices.add(totalPrice);
		}
		request.setAttribute("prices", prices);
		request.setAttribute("pager", pager);
		request.setAttribute("list", list);
		request.getRequestDispatcher("/manage/order/list.jsp").forward(request, response);
	}
	protected void getOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(8);
		int rows=OrderDAO.getOrderdao().getRows();
		pager.setPageCount(rows);
		pager.setPageNo(Integer.parseInt(pageNo));
		List<Order> list=OrderDAO.getOrderdao().getOrders(pager);
		List<Double> prices=new ArrayList<Double>();
		for(Order order:list) {
			//Double prices=OrderDAO.getOrderdao().getPrice(order);
			Boolean bflag = order.getBreakfast();
			Boolean carflag = order.getCar();
			
			Breakfast b=BreakfastDAO.getBreakfastdao().findall();
			Double carPrice=CarDAO.getCarDAO().findprice();
			Double discount=UserinfoDAO.getUserinfodao().findDiscountByUserinfoId(order.getUserinfo().getId());
			Double roomTypePrice= RoomTypeDAO.getRoomtypedao().getPriceByRoomTypeTitle(order.getRoomType().getTitle());
			int days=OrderDAO.getOrderdao().getDays(order.getId());
			double totalPrice =0;
			if(bflag) {
				totalPrice+=b.getPrice()*days;
			}
			if(carflag) {
				totalPrice+=carPrice;
			}
			
			totalPrice=(totalPrice+roomTypePrice*days)*discount;
			
			BigDecimal   bbbb   =   new   BigDecimal(totalPrice); 
			totalPrice   =   bbbb.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 
			
			prices.add(totalPrice);
		}
		request.setAttribute("prices", prices);
		request.setAttribute("pager", pager);
		request.setAttribute("list",list);
		request.getRequestDispatcher("/manage/order/showOrderList.jsp").forward(request, response);
	}

}
